﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
using System.Web.Security;

public partial class Admin_ViewPerformanceUser : System.Web.UI.Page
{
    DataTable dtChart = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindGrid(Request.QueryString["UserId"]);
            bindChart();
        }
    }

    private void bindChart()
    {
        Series series = new Series("Spline");
        series.ChartType = SeriesChartType.Spline;
        series.BorderWidth = 3;
        series.ShadowOffset = 2;

        //series.Points.AddY(67);
        //series.Points.AddY(57);
        //series.Points.AddY(83);
        //series.Points.AddY(23);
        //series.Points.AddY(70);
        //series.Points.AddY(60);
        //series.Points.AddY(90);
        //series.Points.AddY(20);

        //// Add series into the chart's series collection
        //Chart1.Series.Add(series);

        DataTable dtUserName = new DataTable();
        dtUserName = AdminCreateTestDAL.GetUserName(Request.QueryString["UserId"]);
        //Chart1.Titles["Title1"].Text = dtUserName.Rows[0][0].ToString();

        MembershipUser user = Membership.GetUser(dtUserName.Rows[0][0].ToString());
        ProfileCommon profile = (Profile as ProfileCommon).GetProfile(user.UserName);
        if (profile.FirstName.Length == 0)
            Chart1.Titles["Title1"].Text = user.UserName;
        else
            Chart1.Titles["Title1"].Text = profile.FirstName + " " + profile.LastName;

        
        dtChart = AdminCreateTestDAL.GetChartInformation(Request.QueryString["UserId"]);
        Chart1.DataSource = dtChart;

        // Set series members names for the X and Y values 
        Chart1.Series["Series1"].XValueMember = "Test";//dtChart.Rows[0][0].ToString();
        Chart1.Series["Series1"].YValueMembers = "Percentage";//dtChart.Rows[0][1].ToString();
        Chart1.Series["Series1"].BorderWidth = 5;
        Chart1.Series["Series1"].Color = System.Drawing.Color.Green;
        //Chart1.Series["Series1"].Points.DataBindXY(dtChart.Rows, "Test", dtChart.Rows, "Percentage");//.DataBind(dtChart.Rows, "Test", "Percentage", "Tooltip=Test");
        //Data bind to the selected data source
        Chart1.DataBind();


    }

    private void bindGrid(object userId)
    {
        DataTable dt = new DataTable();
        dt = AdminCreateTestDAL.GetStudentPerformanceRecordBasedOnUserId(userId);
        gvStudent.DataSource = dt;
        gvStudent.DataBind();
    }
    protected void Chart1_DataBound(object sender, EventArgs e)
    {
        foreach (DataPoint dp in Chart1.Series["Series1"].Points)
        {
            string s = ((System.Web.UI.DataVisualization.Charting.DataPointCustomProperties)(dp)).AxisLabel.ToString();
            string testName = "";
            for (int i = 0; i < dtChart.Rows.Count; i++)
            {
                if (s == dtChart.Rows[i][0].ToString())
                    testName = dtChart.Rows[i][2].ToString();

            }
            dp.ToolTip = testName + " - " + dp.YValues[0].ToString()+"%"; 
            dp.MarkerSize = 10;
            dp.MarkerColor = System.Drawing.Color.Red;
            dp.MarkerStyle = MarkerStyle.Circle;
        }

    }
}